package io.framework.modules.water.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.common.utils.DateUtils;
import io.framework.modules.station.dto.StationDTO;
import io.framework.modules.water.dao.WaterMonitorDao;
import io.framework.modules.water.dto.WaterMonitorDTO;
import io.framework.modules.water.entity.WaterMonitorEntity;
import io.framework.modules.water.service.WaterMonitorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 城镇内河(湖)水环境主要监测结果
 *
 * @author Gujq email
 * @since 1.0.0 2020-05-25
 */
@Service
public class WaterMonitorServiceImpl extends CrudServiceImpl<WaterMonitorDao, WaterMonitorEntity, WaterMonitorDTO> implements WaterMonitorService {

    @Override
    public QueryWrapper<WaterMonitorEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<WaterMonitorEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    @Override
    public PageData<WaterMonitorDTO> page(Map<String, Object> params) {

        IPage<WaterMonitorEntity> pages = getPage(params, Constant.CREATE_DATE, false);

        List<WaterMonitorEntity> list = baseDao.getList(params);

        return getPageData(list, pages.getTotal(), WaterMonitorDTO.class);
    }

    @Override
    public PageData<WaterMonitorDTO> pageList(Map<String, Object> params) {
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
        String orderField = jsonObject.getString("orderField");
        String order = jsonObject.getString("order");
        String time = jsonObject.getString("time");
        Integer type = jsonObject.getInteger("type");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        QueryWrapper<WaterMonitorEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(time)) {
            queryWrapper.eq("date_format(month,'%Y-%m')", time);
        }
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        Page<WaterMonitorEntity> p = new Page<>(page, limit);
        if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)) {
            if (order.equals("desc")) {
                queryWrapper.orderByDesc(orderField);
            } else {
                queryWrapper.orderByAsc(orderField);
            }
        }
        IPage<WaterMonitorEntity> ip = baseDao.selectPage(p, queryWrapper);
        List<WaterMonitorDTO> dtoList = JSONArray.parseArray(JSONArray.toJSONString(ip.getRecords()), WaterMonitorDTO.class);
        return new PageData<WaterMonitorDTO>(dtoList, ip.getTotal());
    }

    @Override
    public List<Map<String, Object>> getStationName() {
        return baseDao.getStationName();
    }

    @Override
    public List<Map<String, Object>> getWaterndMonthChange(Map<String, Object> params) {
        if (params.get("startDate")==null) {
            Integer dateType = Integer.parseInt(params.get("dateType").toString());
            String date = params.get("date").toString();
            String endDate = "", startDate = "";
            int day = 0;
            if (dateType == 1) {
                //年
                date = DateUtils.format(DateUtils.parse(date, "yyyy"), "yyyy");
                startDate = date + "-01-01";
                endDate = date + "-12-31";
            } else if (dateType == 2) {
                int yyyy = Integer.parseInt(date.substring(0, date.indexOf("-")));
                int q = Integer.parseInt(date.substring(date.indexOf("Q") + 1, date.length()));
                if (q == 1) {
                    startDate = yyyy + "-01-01";
                    endDate = yyyy + "-03-31";
                } else if (q == 2) {
                    startDate = yyyy + "-04-01";
                    endDate = yyyy + "-06-31";
                } else if (q == 3) {
                    startDate = yyyy + "-07-01";
                    endDate = yyyy + "-09-31";
                } else if (q == 4) {
                    startDate = yyyy + "-10-01";
                    endDate = yyyy + "-12-31";
                }
            } else if (dateType == 3) {
                date = date + "-01";
                //一个月有多少天
                day = DateUtils.getDaysOfMonth(DateUtils.parse(date, DateUtils.DATE_MONTH_PATTERN), Calendar.DAY_OF_MONTH);
                startDate = DateUtils.getMonthBegin(date);
                endDate = DateUtils.getMonthEnd(date);
            }
            params.put("startDate", startDate);
            params.put("endDate", endDate);
        }
        List<Map<String, Object>> resultMapList = new ArrayList<>();
        String stationName[] = params.get("stationName").toString().replaceAll(" ", "").split(",");
        for (String name : stationName) {
            Map<String, Object> map = new HashMap<>();
            params.put("stationName", name);
            List<Map<String, Object>> list = baseDao.getWaterndMonthChange(params);

            map.put("stationName", name);
            map.put("listData", list);
            resultMapList.add(map);
        }
        return resultMapList;
    }

    @Override
    public WaterMonitorDTO getWaterInfoById(Map<String, Object> params) {

        WaterMonitorEntity entity = baseDao.getWaterInfoById(params);

        return ConvertUtils.sourceToTarget(entity, WaterMonitorDTO.class);
    }

    @Override
    public void deleteInfoByMonth(String month) {

        baseDao.deleteInfoByMonth(month);
    }

    @Override
    public Boolean checkData(String month) {
        Integer count = baseDao.checkData(month);
        return count > 0 ? true : false;
    }

}
